﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using HeThongThongTin.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using HeThongThongTin.UI.SL.DanhMuc.DM_PhongBan;
using HeThongThongTin.UI.SL.DanhMuc.DM_GiayTo;

namespace HeThongThongTin.UI.SL.DanhMuc.DM_LoaiHoSo
{
    public partial class DM_LoaiHoSo_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "LoaiHoSo"; }
        }

       
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public DM_LoaiHoSo_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }

        public DM_LoaiHoSo_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("HeThongThongTin.UI.SL.DanhMuc.DM_LoaiHoSo.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.DM_LoaiHoSo_GeneralInformation_GroupBox, controls);
           
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource DM_LoaiHoSo_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoSo_ViewModelSource"];
                if (Khoa == null)
                {
                    DM_LoaiHoSo_ViewModel DM_LoaiHoSo_ViewModel = new DM_LoaiHoSo_ViewModel();
                    DM_LoaiHoSo_ViewModel.ChildList_AddedNew += new DM_LoaiHoSo_ViewModel.ChildList_AddedNew_EventHandler(DM_LoaiHoSo_ViewModel_ChildList_AddedNew);
                    DM_LoaiHoSo_CollectionViewSource.Source = new List<object> { DM_LoaiHoSo_ViewModel };    
                }
                else
                {
                    DM_LoaiHoSo_ViewModel DM_LoaiHoSo_ViewModel = new DM_LoaiHoSo_ViewModel((int)Khoa);
                    DM_LoaiHoSo_ViewModel.ChildList_AddedNew += new DM_LoaiHoSo_ViewModel.ChildList_AddedNew_EventHandler(DM_LoaiHoSo_ViewModel_ChildList_AddedNew);
                    DM_LoaiHoSo_CollectionViewSource.Source = new List<object> { DM_LoaiHoSo_ViewModel };    
                }

                System.Windows.Data.CollectionViewSource DM_PhongBan_ListCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhongBan_ListViewModelSource"];
                DM_PhongBan_ListViewModel dm_PhongBan_ListViewModel = new DM_PhongBan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                DM_PhongBan_ListCollectionViewSource.Source = new List<object> { dm_PhongBan_ListViewModel };

                System.Windows.Data.CollectionViewSource DM_GiayTo_ListCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_GiayTo_ListViewModelSource"];
                DM_GiayTo_ListViewModel dm_GiayTo_ListViewModel = new DM_GiayTo_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                DM_GiayTo_ListCollectionViewSource.Source = new List<object> { dm_GiayTo_ListViewModel };

                this.DM_LoaiHoSo_GiayToKemTheoGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.DM_LoaiHoSo_GiayToKemTheoGridView);   
            }
        }

        #endregion
       

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                DM_LoaiHoSo_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.DM_LoaiHoSo_GiayToKemTheoTab.IsSelected = true;
                }
                else if (e.Key == Key.D2)
                {
                    this.DM_LoaiHoSo_QuyTrinhXuLyTab.IsSelected = true;
                }
               
               
                
            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
            if (this.DM_LoaiHoSo_GiayToKemTheoTab.IsSelected)
            {
                AddGiayToKemTheoDetailsItem();
            }
            else if (this.DM_LoaiHoSo_QuyTrinhXuLyTab.IsSelected)
            {
                AddQuyTrinhXuLyDetailsItem();
            }
        }
        private RadGridView GetActiveGridView()
        {
            if (this.DM_LoaiHoSo_GiayToKemTheoTab.IsSelected)
            {
                return this.DM_LoaiHoSo_GiayToKemTheoGridView;
            }
            else if (this.DM_LoaiHoSo_QuyTrinhXuLyTab.IsSelected)
            {
                return this.DM_LoaiHoSo_QuyTrinhXuLyGridView;
            }
            return null;
        }

        void DeleteItems()
        {
            if (this.DM_LoaiHoSo_GiayToKemTheoTab.IsSelected)
            {
                DM_LoaiHoSo_GiayToKemTheoChild ChiTietChild = (DM_LoaiHoSo_GiayToKemTheoChild)(this.DM_LoaiHoSo_GiayToKemTheoGridView.SelectedItem);
                IList<DM_LoaiHoSo_GiayToKemTheoChild> list = this.GetViewModel().Model.DM_LoaiHoSo_GiayToKemTheos;
                int index = list.IndexOf(ChiTietChild);
                DeleteGiayToKemTheoDetailsItem(index);
            }
            else if (this.DM_LoaiHoSo_GiayToKemTheoTab.IsSelected)
            {
                DM_LoaiHoSo_QuyTrinhXuLyChild ChiTietChild = (DM_LoaiHoSo_QuyTrinhXuLyChild)(this.DM_LoaiHoSo_QuyTrinhXuLyGridView.SelectedItem);
                IList<DM_LoaiHoSo_QuyTrinhXuLyChild> list = this.GetViewModel().Model.DM_LoaiHoSo_QuyTrinhXuLys;
                int index = list.IndexOf(ChiTietChild);
                DeleteQuyTrinhXuLyDetailsItem(index);

            }
            

        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiHoSo_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
        }


        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {

            Dispatcher.BeginInvoke(new Action(() =>
            {
                grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                grid.Focus();
                grid.BeginEdit();
            }));


        }

        #endregion

        #region View Model Related Methods
        private DM_LoaiHoSo_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource DM_LoaiHoSo_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoSo_ViewModelSource"];
            return ((List<object>)DM_LoaiHoSo_CollectionViewSource.Source)[0] as DM_LoaiHoSo_ViewModel;
        }

        private void AddQuyTrinhXuLyDetailsItem()
        {
            GetViewModel().AddDM_LoaiHoSo_QuyTrinhXuLyItem();           
        }

        private void AddGiayToKemTheoDetailsItem()
        {
            GetViewModel().AddDM_LoaiHoSo_GiayToKemTheoItem();
        }
        private void DeleteGiayToKemTheoDetailsItem(int Index)
        {
            GetViewModel().DeleteDM_LoaiHoSo_GiayToKemTheoItem( Index);
        }

        private void DeleteQuyTrinhXuLyDetailsItem(int Index)
        {
            GetViewModel().DeleteDM_LoaiHoSo_QuyTrinhXuLyItem(Index);
        }
        void DM_LoaiHoSo_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "DM_LoaiHoSo_GiayToKemTheos")
            {
                this.DM_LoaiHoSo_GiayToKemTheoGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            if (e.ChildListName == "DM_LoaiHoSo_QuyTrinhXuLys")
            {
                this.DM_LoaiHoSo_QuyTrinhXuLyGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            } 
        }
        #endregion
        
    }
}
